package cn.ecut.controller;

import cn.ecut.model.User;
import cn.ecut.service.UserService;
import cn.ecut.utils.CodeUtil;
import cn.ecut.utils.MobileNO;
import cn.ecut.utils.RandomUtil;
import cn.ecut.utils.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
/*@RequestMapping("/user")*/
@SessionAttributes("user")
public class UserController {
    @Autowired
    UserService userService;

    //登入
    @RequestMapping("/loginCheck")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Model model) {
     Map map = new HashMap();
     if(MobileNO.isMobileNO(username)){
         map.put("phoneNumber",username);
     }else{
         map.put("username",username);
     }
     map.put("password",password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        if (userService.login(map) != null) {
            //为了将用户除了用户名和密码之外的信息存入session，将数据库中的内容读取出来
            user = userService.login(map);
            model.addAttribute("user", user);
            return "user/userIndex"; //登入成功，进入主页；
        }
        return "user/userLogin"; //登入失败，重新登入
    }

    @RequestMapping("/register")//注册
    public String register(@RequestParam("username") String username,
                           @RequestParam("password") String password,
                           @RequestParam("phoneNumber") String phoneNumber,
                           @RequestParam("verificationCode") int code,
                           Model model) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhoneNumber(phoneNumber);
        user.setUserId(Integer.toString(RandomUtil.getRandNum()));
        if (code == SendSms.code) {
            userService.register(user);
            return "user/registerSuccess";
        } else
            return "user/userRegister";
    }

    @RequestMapping(value = "alterPwd")
    public String alterPwd(@RequestParam("username") String username,
                           @RequestParam("password") String password,
                           @RequestParam("verificationCode") int code) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        System.out.println(user);
        if (code == SendSms.code) {
            userService.alterPwd(user);
            return "user/alterPwdSuccess";
        } else
            return "user/userLogin";
    }

    //触发发送验证码
    @RequestMapping(value = "/sendMsg/{forget}")
    public String test(@PathVariable String forget) {
        System.out.println("test");
        SendSms.sendMessage(forget);
        return Integer.toString(SendSms.code);
    }

    //更新用户信息
    @RequestMapping(value = "/updateUserInfo")
    public String updateUserInfo(@RequestParam("userId") String userId,
                                 @RequestParam("username") String username,
                                 @RequestParam("sex") String sex,
                                 @RequestParam("birthday") String birthday,
                                 @RequestParam("phoneNumber") String phoneNumber,
                                 @RequestParam("address") String address,
                                 @SessionAttribute("user") User user) {
        user.setUserId(userId);
        if (username != "") {
            user.setUsername(username);
        }
        if (sex != "") {
            user.setSex(sex);
        }
        if (birthday != "") {
            user.setBirthday(birthday);
        }
        if (phoneNumber != "") {
            user.setPhoneNumber(phoneNumber);
        }
        if (address != "") {
            user.setAddress(address);
        }
        userService.updateUserInfo(user);
        return "user/personalCenter";
    }
}
